package gui.CustomerServiceWorker;
import gui.ParkingLotWorker.SetParkingStatusScreen;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.ParkingFloor;
import vcpClasses.ParkingLocation;
import vcpClasses.ParkingLot;
import vcpClasses.ParkingRow;
import vcpClasses.ParkingSpace;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;

import com.toedter.calendar.JDateChooser;

import control.LocalOpertaionController;
import control.LoginController;
import javax.swing.DefaultComboBoxModel;
/**
 * this class use  for Customer Service Save Parking Screen 
 * @author G8
 *
 */
public class CustomerSaveParkingScreen extends JPanel implements ActionListener{
	private JButton btnSave;
	private LocalOpertaionController localOpControl;
	private JLabel lblFloorNumber;
	private JLabel lblRowNumber;
	private JLabel lblSpaceNumber;
	private JComboBox comboBoxFloorNumber;
	private JComboBox comboBoxRowNumber;
	private JTextField txtSpaceNumber;
	private JComboBox comboBoxStatus;
	private JCheckBox chckbxEntireFloor;
	private JCheckBox chckbxEntireRow;
	private JCheckBox chckbxEntireLot;
	private SystemTypes deviceToSet;
	private SystemTypes deviceSetResult;
	private JComboBox comboBoxCurrLot;
	private JButton btnBack;
	private JButton btnLogout;
	private JButton btnSetStatus;

	/**
	 *Customer Service Save Parking Screen  Constructor 
	 */
	public CustomerSaveParkingScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
		public void initialize(){
			localOpControl = new LocalOpertaionController(null);
			setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Set Parking Status", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
			setBackground(SystemColor.menu);
			setBounds(VCP.guiX,VCP.guiY,500,400);
			setLayout(null);
			VCP.frame.setBounds(this.getBounds());
			
			btnBack = new JButton("Back");
			btnBack.setIcon(new ImageIcon(CustomerSaveParkingScreen.class.getResource("/img/back.png")));
			btnBack.setSize(150, 30);
			btnBack.setBounds(281, 22, 102, 35);
			btnBack.addActionListener(this);
			add(btnBack);
			
			btnLogout = new JButton("Logout");
			btnLogout.setSize(150, 30);
			btnLogout.setBounds(393, 22, 78, 35);
			btnLogout.addActionListener(this);
			add(btnLogout);
			
			btnSetStatus = new JButton("Set Status");
			btnSetStatus.setBounds(206, 284, 110, 23);
			btnSetStatus.addActionListener(this);
			add(btnSetStatus);
			
			JLabel lblLotNumber = new JLabel("Lot Number:");
			lblLotNumber.setBounds(105, 122, 110, 14);
			add(lblLotNumber);
			
			lblFloorNumber = new JLabel("Floor Number:");
			lblFloorNumber.setBounds(105, 151, 110, 14);
			add(lblFloorNumber);
			
			lblRowNumber = new JLabel("Row Number:");
			lblRowNumber.setBounds(105, 179, 100, 14);
			add(lblRowNumber);
			
			lblSpaceNumber = new JLabel("Space Number:");
			lblSpaceNumber.setBounds(105, 204, 110, 14);
			add(lblSpaceNumber);
			
			comboBoxCurrLot = new JComboBox();
			comboBoxCurrLot.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6"}));
			comboBoxCurrLot.setBounds(227, 122, 89, 20);
			comboBoxCurrLot.addActionListener(this);
			add(comboBoxCurrLot);
			VCP.currentLot = new ParkingLot(1); // default lot no.1
			
			comboBoxFloorNumber = new JComboBox();
			comboBoxFloorNumber.setBounds(227, 149, 89, 20);
			for (int i=1;i<4;i++)
				comboBoxFloorNumber.addItem(i);
			add(comboBoxFloorNumber);
			
			comboBoxRowNumber = new JComboBox();
			comboBoxRowNumber.setBounds(227, 177, 89, 20);
			for (int i=1;i<4;i++)
				comboBoxRowNumber.addItem(i);
			add(comboBoxRowNumber);
			
			txtSpaceNumber = new JTextField();
			txtSpaceNumber.setBounds(227, 205, 89, 20);
			add(txtSpaceNumber);
			
			JLabel lblPleaseChooseThe = new JLabel("Please choose the parking space you want to set.");
			lblPleaseChooseThe.setBounds(105, 83, 307, 14);
			add(lblPleaseChooseThe);
			
			JLabel lblStatus = new JLabel("Status:");
			lblStatus.setBounds(105, 255, 78, 14);
			add(lblStatus);
			
			comboBoxStatus = new JComboBox();
			comboBoxStatus.setBounds(227, 253, 89, 20);
			comboBoxStatus.addItem("Available");
			comboBoxStatus.addItem("Reserved");
			add(comboBoxStatus);
			
			chckbxEntireFloor = new JCheckBox("Entire Floor");
			chckbxEntireFloor.setBounds(331, 148, 97, 23);
			chckbxEntireFloor.addActionListener(this);
			add(chckbxEntireFloor);
			
			chckbxEntireRow = new JCheckBox("Entire Row");
			chckbxEntireRow.setBounds(331, 176, 97, 23);
			chckbxEntireRow.addActionListener(this);
			add(chckbxEntireRow);
			
			chckbxEntireLot = new JCheckBox("Entire Lot");
			chckbxEntireLot.setBounds(331, 119, 97, 23);
			chckbxEntireLot.addActionListener(this);
			add(chckbxEntireLot);		
	}
	
		
		/**
		 * This method listens to the buttons on the panel
		 * @param e is the buttons the user Click
		 */
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == comboBoxCurrLot){
				VCP.currentLot = new ParkingLot((int)comboBoxCurrLot.getSelectedItem());
			}
			if (e.getSource() == chckbxEntireLot){
				if (chckbxEntireLot.isSelected()==true){
					chckbxEntireFloor.setEnabled(false);
					chckbxEntireRow.setEnabled(false);
					comboBoxFloorNumber.setEnabled(false);
					comboBoxRowNumber.setEnabled(false);
					txtSpaceNumber.setEnabled(false);
				}
				else{
					chckbxEntireFloor.setEnabled(true);
					chckbxEntireRow.setEnabled(true);
					comboBoxFloorNumber.setEnabled(true);
					comboBoxRowNumber.setEnabled(true);
					txtSpaceNumber.setEnabled(true);
				}
			}
			if (e.getSource() == chckbxEntireFloor){
				if (chckbxEntireFloor.isSelected()==true){
					chckbxEntireLot.setEnabled(false);
					chckbxEntireRow.setEnabled(false);
					comboBoxRowNumber.setEnabled(false);
					txtSpaceNumber.setEnabled(false);
				}
				else{
					chckbxEntireLot.setEnabled(true);
					chckbxEntireRow.setEnabled(true);
					comboBoxRowNumber.setEnabled(true);
					txtSpaceNumber.setEnabled(true);
				}
			}
			if (e.getSource() == chckbxEntireRow){
				if (chckbxEntireRow.isSelected()==true){
					chckbxEntireLot.setEnabled(false);
					chckbxEntireFloor.setEnabled(false);
					txtSpaceNumber.setEnabled(false);
				}
				else{
					chckbxEntireLot.setEnabled(true);
					chckbxEntireFloor.setEnabled(true);
					txtSpaceNumber.setEnabled(true);
				}
			}
			if (e.getSource() == btnSetStatus){
				
				txtSpaceNumber.setBackground(Color.WHITE);
				
				if (FieldValidator.checkSpaceNumber(txtSpaceNumber.getText()))
				{
					SystemMode deviceMode=null;
					if (comboBoxStatus.getSelectedItem().toString().equals("Available"))
						deviceMode=SystemMode.SET_FREE;
					if (comboBoxStatus.getSelectedItem().toString().equals("Reserved"))
						deviceMode=SystemMode.SET_SAVED;
					if (chckbxEntireLot.isSelected()==true){
						deviceToSet = new SystemTypes(VCP.currentLot,deviceMode);
					}
					else if (chckbxEntireFloor.isSelected()==true){
						ParkingFloor newParkingFloor = new ParkingFloor(VCP.currentLot.getParkingLotNumber(),(int)comboBoxFloorNumber.getSelectedItem());
						deviceToSet = new SystemTypes(newParkingFloor,deviceMode);
					}
					else if (chckbxEntireRow.isSelected()==true){
						ParkingRow newParkingRow = new ParkingRow((int)comboBoxRowNumber.getSelectedItem(),(int)comboBoxFloorNumber.getSelectedItem(),VCP.currentLot.getParkingLotNumber());
						deviceToSet = new SystemTypes(newParkingRow,deviceMode);
					}
					else{
						ParkingLocation newParkingSpace = new ParkingLocation(VCP.currentLot.getParkingLotNumber(),(int)comboBoxFloorNumber.getSelectedItem(),(int)comboBoxRowNumber.getSelectedItem(),Integer.parseInt(txtSpaceNumber.getText()));
						deviceToSet = new SystemTypes(newParkingSpace,deviceMode);
					}
					deviceSetResult = localOpControl.setDeviceDisableStatus(deviceToSet);
					if (deviceToSet.getSysMode()==SystemMode.OK){
						JOptionPane.showMessageDialog(null, deviceSetResult.getObj().toString(), "Success",
			                    JOptionPane.PLAIN_MESSAGE);
					}
					else{
						JOptionPane.showMessageDialog(null, deviceSetResult.getObj().toString(), "Error",
			                    JOptionPane.ERROR_MESSAGE);
					}
				}
				else{
					txtSpaceNumber.setBackground(Color.RED);
					JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
				}
			}
			if (e.getSource() == btnBack){
				VCP.setScreen(this, VCP.prevScreen);
				VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
				VCP.prevScreen.setLayout(null);
				VCP.frame.setBounds(VCP.prevScreen.getBounds());
			}
			if (e.getSource() == btnLogout){
				SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
				boolean logoutRes = LoginController.LogOut(userToLogout);
				if (logoutRes)
					VCP.setGUI(0, this);
				else{
					JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
		                    JOptionPane.ERROR_MESSAGE);
				}
			}
		
	}
}
